package com.redshark.wmm.UI;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.redshark.wmm.R;
import com.redshark.wmm.MeasurementProcessing.Measurement;

public class SingleValueFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.single_value_fragment, container,
				false);
	}

	public void setValue(Measurement m) {
		TextView valueTV = (TextView) getView()
				.findViewById(R.id.measurementTV);
		TextView labelTV = (TextView) getView().findViewById(R.id.unitsTV);

		if (m.getValue() != Double.POSITIVE_INFINITY) {
			BigDecimal bdValue = BigDecimal.valueOf(m.getValue());
			BigDecimal wantedValue = bdValue.round(new MathContext(m
					.getSigFigs(), RoundingMode.HALF_UP));
			valueTV.setText(wantedValue.toEngineeringString());
		} else {
	    	valueTV.setText(Double.toString(m.getValue()));
		}

		labelTV.setText(m.getUnits());
	}

}
